<template>
	<view class="page">
		<view class="page-box">
			<view class="btnBox">
				<view class="title">
					<image class="tex" src="@/static/logo.png" mode=""></image>
					<view class="name">益来科智慧能源</view>
				</view>

				<view class="choose-nickname-row">
					<input v-model="form.account" type="number" placeholder="登录账号" />
				</view>
				<!-- <view class="choose-nickname-row" v-if="t">
					{{t}}
				</view> -->
				<view class="choose-nickname-row">
					<input v-model="form.password" type="text" placeholder="登录密码" password />
				</view>
				<!-- <view class="choose-nickname-row" style="display: flex;justify-content: space-between;">
					<vvSelect :list="allList" label="name" valueKey="id" :placeholder="placeholderText" ref="vvSelect"
						@change="tabChange" style="width: 86%;">
					</vvSelect>
					<view class="" style="margin-left: 8rpx;">
						<u-icon name="arrow-down" size="16" v-if="!closeIcon"></u-icon>
						<u-icon name="close-circle" color="#807E7E;" v-else size="18" @click="closeSearch"
							style="margin-left: 10rpx;"></u-icon>
					</view>
				</view> -->
			</view>
			<view class="" style="width: 100%;">
				<But @tap.stop="handleLogin" text="登录"></But>
			</view>
			<view class="clientsMobile" @tap.stop="callPhone">
				平台电话:{{phone||'-'}}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		weixinLogin
	} from "@/request/myApi.js";
	import {
		companyPhone
	} from "@/request/homeApi.js";
	import But from "@/components/Button/index.vue"
	import vvSelect from '@/components/vv-select/index.vue'
	export default {
		components: {
			But,
			vvSelect
		},
		data() {
			return {
				form: {
					account: '',
					password: '',
					// group_id: '',
					user_id: ''
				},
				allList: [{
						name: '外部',
						id: 2
					},
					{
						name: '员工',
						id: 1
					}
				],
				placeholderText: '选择身份',
				closeIcon: false,
				phone: '',
				t:'',
				// coo:""
			};
		},
		onLoad(e) {
			// this.coo = e ?e:''
			// uni.showToast({
			// 	title:e.user_id,
			// 	icon:'none',
			// 	duration:2000
			// })
			console.log(e,'eeeeeeeeeeeeeeeeeeeee')
			this.getMemberList()
			if (e.q && e.q != "undefined") { //扫码进入
				const qrUrl = decodeURIComponent(e.q) // 获取到二维码原始链接内容
				//GetWxMiniProgramUrlParam() 这个方法是解析链接地址中的参数方法
				e = this.GetWxMiniProgramUrlParam(qrUrl); //此处就是我们要获取的参数 json
				//比如我要得到user_id的值，直接取值即可
				uni.setStorageSync('user_id', e.user_id)
				this.t = uni.getStorageSync('user_id')
			}
			if (e.user_id) {
				uni.setStorageSync('user_id', e.user_id)
			}
		},
		methods: {
			GetWxMiniProgramUrlParam(url) {
				let theRequest = {};
				if (url.indexOf("#") != -1) {
					const str = url.split("#")[1];
					const strs = str.split("&");
					for (let i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
					}
				} else if (url.indexOf("?") != -1) {
					const str = url.split("?")[1];
					const strs = str.split("&");
					for (let i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
					}
				}
				return theRequest;
			},
			// 点击拨号
			callPhone() {
				uni.makePhoneCall({
					phoneNumber: this.phone,
				});
			},
			// 选择身份
			tabChange(e) {
				this.form.group_id = e
				this.closeIcon = true
			},
			// 清除身份
			closeSearch() {
				this.$refs.vvSelect.emptCheckLabels()
				this.form.group_id = ''
				this.closeIcon = false
			},
			// 登录
			handleLogin() {
				var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
				if (this.form.account == '') {
					uni.showToast({
						title: '请输入账号',
						duration: 1500,
						icon: 'none',
					})
					return
				} else if (!reg_tel.test(this.form.account)) {
					uni.showToast({
						title: '请正确填写账号',
						duration: 1500,
						icon: 'none',
					})
					return
				} else if (this.form.password == '') {
					uni.showToast({
						title: '请输入密码',
						duration: 1500,
						icon: 'none',
					})
					return
				} else if (this.form.password.length < 6) {
					uni.showToast({
						title: '密码大于等于六位',
						duration: 1500,
						icon: 'none',
					})
					return
				} 
				// else if (this.form.group_id == '') {
				// 	uni.showToast({
				// 		title: '请选择身份',
				// 		duration: 1500,
				// 		icon: 'none',
				// 	})
				// 	return
				// }
				// uni.setStorageSync('user_id', 15)
				this.form.user_id = uni.getStorageSync('user_id')?uni.getStorageSync('user_id'):''
				console.log(this.form,'this.form')
				weixinLogin(this.form).then((res) => {
					if (res.code == 200) {
						uni.setStorageSync("userInfo", JSON.stringify(res.data.userinfo));
						uni.setStorageSync("token", res.data.userinfo.token);
						uni.showToast({
							title: '登录成功',
							duration: 1500,
							icon: 'none',
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/tabBar/homeIndex'
							})
						}, 1000);
					} else {
						uni.showToast({
							title: res.msg,
							duration: 1500,
							icon: 'none',
						})
					}
				});
			},
			// 获取会员信息
			getMemberList() {
				companyPhone().then((res) => {
					if (res.code == 200) {
						this.phone = res.data[0].value
					} else {
						uni.showToast({
							title: res.msg,
							duration: 1500,
							icon: 'none',
						})
					}
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	button::after {
		border: none;
		background-color: transparent !important;
		padding: 0;
	}

	.clientsMobile {
		width: 85%;
		height: 60rpx;
		line-height: 60rpx;
		position: fixed;
		bottom: 20rpx;
		text-align: center;
		color: #58B99D;
		font-size: 24rpx;
	}

	.page {
		background-size: 100% 100%;
		background-image: url("");
		height: 100%;
		background-repeat: no-repeat;
		position: fixed;
		top: 0;
		overflow: hidden;
		left: 0;
		width: 100%;

		&-box {
			height: 700rpx;
			margin: 250rpx auto;
			width: 640rpx;
			display: flex;
			flex-direction: column;
			justify-items: center;

			.btn {
				font-size: 28rpx;
				background-color: #fff;
				border: 1rpx solid #dddddd;
				padding: 20rpx;
				border-radius: 8rpx;

				&:active {
					background-color: #f2f2f2;
				}
			}
		}
	}


	// .button {
	// 	background-color: #466EC5;
	// 	width: 100%;
	// 	color: #fff;
	// 	border-radius: 40rpx;
	// 	height: 88rpx;
	// 	line-height: 88rpx;
	// 	font-size: 30rpx;
	// 	margin-top: 100rpx;
	// }

	.btnBox {
		width: 100%;
		height: 600rpx;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;

		.title {
			display: flex;
			align-items: center;
			height: 100rpx;
			width: 66%;
			justify-content: space-between;
			margin-bottom: 50rpx;

			.tex {
				width: 100rpx;
				height: 100%;
			}

			.name {
				font-size: 42rpx;
				color: #fff;
			}
		}


		.choose-nickname-row {
			width: 100%;
			height: 9%;
			padding: 62rpx 4rpx 7rpx;
			text-align: left;
			font-size: 30rpx;
			margin: 0 auto;
			border-bottom: 1px solid #F2F2F2;

			.input {
				color: #807E7E;
			}
		}
	}
</style>